package team.pwy.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team.pwy.pojo.entity.Message;
import team.pwy.service.MessageService;
import team.pwy.vo.Result;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("message")
public class MessageController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MessageService messageService;

    @PostMapping()
    public void message(Integer userId){
        ListOperations lop = redisTemplate.opsForList();
        List messageList = lop.range("usermessage"+userId,0,-1);

        if (messageList != null && messageList.size() > 0){
            redisTemplate.delete("usermessage"+userId);
        }
    }

    @GetMapping()
    public Result getMessageNum(Integer userId){

        ListOperations lop = redisTemplate.opsForList();
        List messageList = lop.range("usermessage" + userId,0,-1);

        if (messageList != null && messageList.size() > 0){
            return new Result("200" , "",messageList.size());
        }
        return new Result("200" , "",0);
    }

    @GetMapping("info")
    public Result getMessageInfo(Integer userId){

        //mysql
        List<String> list = new ArrayList<>();
        list.add("iygh");
        list.add("iutvghbh");

        return new Result("200" , "",list);
    }

    @GetMapping("/findByStatus")
    public Result findByStatus(Integer userId,Integer status,Integer pageNum,Integer pageSize){
        PageInfo<Message> messageList = messageService.findByStatus(userId,status ,pageNum,pageSize);
        if (messageList != null && !messageList.getList().isEmpty()){
            return new Result("200","未读信息查询成功",messageList);
        }
        return new Result("400","没有未读信息",messageList);
    }
    @GetMapping("/findAllMsg")
    public Result findAllMsg(Integer userId,Integer pageNum,Integer pageSize){
        PageInfo<Message> messageList = messageService.findAllMsg(userId,pageNum,pageSize);
        if (messageList != null && messageList.getList().size() != 0){
            return new Result("200","全部信息查询成功",messageList);
        }
        return new Result("400","没有信息",messageList);
    }

    @PostMapping("updateByLookReal")
    public void updateByLookReal(Integer userId){
        messageService.updateByLookReal(userId);
    }
}
